package com.yasaka.stock.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.UUID;
import java.util.regex.Pattern;

public class TUtils {

	public static String getFileCharacterEnding(String aFileName) {
		File mFile = new File(aFileName);
		InputStream inputStream;
		try {
			inputStream = new FileInputStream(mFile);
			try {
				return getInputStreamEnding(inputStream);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String getInputStreamEnding(InputStream aInput)
			throws IOException {
//		String dc = Charset.defaultCharset().name();
		UnicodeInputStream uin = new UnicodeInputStream(aInput, "UTF-8");

		if ("UTF-8".equals(uin.getEncoding())) {
//			uin.close();
			return "UTF-8";
		}
//		uin.close();

		byte[] head = new byte[3];
		aInput.read(head);
		String code = "GBK";
		if (head[0] == -1 && head[1] == -2)
			code = "UTF-16";
		if (head[0] == -2 && head[1] == -1)
			code = "Unicode";
		// 带BOM
		if (head[0] == -17 && head[1] == -69 && head[2] == -65)
			code = "UTF-8";
		if ("Unicode".equals(code)) {
			code = "UTF-16";
		}
		return code;
		// if (aInput == null)
		// return null;
		//
		// byte[] head = new byte[3];
		// try {
		// aInput.read(head);
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// String code = "gbk";
		// if (head[0] == -1 && head[1] == -2) {
		// code = "UTF-16";
		// }
		// if (head[0] == -2 && head[1] == -1) {
		// code = "Unicode";
		// }
		// if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
		// code = "UTF-8";
		// }
		// return code;

	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");

		return pattern.matcher(str).matches();
	}

	public static String createGUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	public static double round(double num, int decimalLength) {
		if (Double.isNaN(num)) {
			return 0;
		}
		BigDecimal bd = new BigDecimal(num);
		num = bd.setScale(decimalLength, BigDecimal.ROUND_HALF_UP)
				.doubleValue();
		return num;
	}

	public static boolean fileIsSupport(String aFileName) {
		String extName = aFileName.substring(aFileName.lastIndexOf("."));
		return ".pdf".equalsIgnoreCase(extName)
				|| ".doc".equalsIgnoreCase(extName)
				|| ".docx".equalsIgnoreCase(extName)
				// || ".ppt".equalsIgnoreCase(extName)
				// || ".pptx".equalsIgnoreCase(extName)
				|| ".xls".equalsIgnoreCase(extName)
				|| ".xlsx".equalsIgnoreCase(extName)
				|| ".txt".equalsIgnoreCase(extName)
				|| ".rtf".equalsIgnoreCase(extName)
				|| ".srt".equalsIgnoreCase(extName)
				|| ".properties".equalsIgnoreCase(extName)
				|| ".xml".equalsIgnoreCase(extName)
				|| ".png".equalsIgnoreCase(extName)
				|| ".tiff".equalsIgnoreCase(extName)
				|| ".jpg".equalsIgnoreCase(extName)
				|| ".bmp".equalsIgnoreCase(extName)
				|| ".jpeg".equalsIgnoreCase(extName)
				|| ".tif".equalsIgnoreCase(extName);
		// || ".dxf".equalsIgnoreCase(extName)
		// || ".dwg".equalsIgnoreCase(extName);
	}
}
